﻿using Framework.SqlDataAccess.Controller;
using Framework.SqlDataAccess.Manager;
using Framework.SqlDataAccess.Model;
using System.Collections.Generic;
using Kaysite.Controller;
using Kaysite.Model;

namespace Kaysite.Manager
{
    public interface IManagerDataSource : IManager
    {
        object Retrieve();
    }

    public delegate void ExceptionCheckDeleagate();

    public abstract class KaysiteManager<C, M> : ManagerBase<C, M>, IManagerDataSource
        where C : KaysiteController<M>, new()
        where M : KaysiteModel, new()
    {
        public CustomFilter Filter { get { return Controller.Filter; } }

        public virtual M InitializeModel(int id)
        {
            M model = Model ?? Controller.Get(id) ?? new M();
            if (Model == null)
                Model = model;
            return model;
        }
        public virtual M Save()
        {
            Validate();
            return Controller.Save();
        }

        public virtual void Delete()
        {
            Controller.Delete();
        }
        public virtual void Delete(int id)
        {
            Controller.Delete(id);
        }

        public virtual bool TryDelete(int id)
        {
            Controller.BeginTransaction();
            try
            {
                Delete(id);
                return true;
            }
            catch
            {
                return false;
            }
            finally
            {
                Controller.RollbackTransaction();
            }
        }

        public List<M> GetAll()
        {
            return GetAll(null, true);
        }
        public List<M> GetAll(string orderFieldName)
        {
            return GetAll(orderFieldName, true);
        }
        public virtual List<M> GetAll(string orderFieldName, bool hideDeleted)
        {
            return Controller.GetAll(orderFieldName, hideDeleted);
        }

        public T TransactionOverlay<T>(TransactionOverlayDeleagate<T> executingMethod)
        {
            return Controller.TransactionOverlay(executingMethod);
        }
        public void TransactionOverlay(TransactionOverlayDeleagate executingMethod)
        {
            Controller.TransactionOverlay(executingMethod);
        }

        #region IManagerDataSource Members

        public object Retrieve()
        {
            return GetAll();
        }

        #endregion
    }
    public abstract class KaysiteMultiManager<C, M> : ManagerBase<C, M>
        where C : KaysiteLinkController<M>, new()
        where M : ModelBaseMultiPK, new()
    {
        public CustomFilter Filter { get { return Controller.Filter; } }

        public virtual M InitializeModel(int id1, int id2)
        {
            M model = Model ?? Controller.Get(id1, id2) ?? new M();
            if (Model == null)
                Model = model;
            return model;
        }
        public virtual M Save()
        {
            Validate();
            return Controller.Save();
        }

        public virtual void Delete()
        {
            Controller.Delete();
        }
        public virtual void Delete(int id1, int id2)
        {
            Controller.Delete(id1, id2);
        }

        public List<M> GetAll()
        {
            return GetAll(null, true);
        }
        public List<M> GetAll(string orderFieldName)
        {
            return GetAll(orderFieldName, true);
        }
        public virtual List<M> GetAll(string orderFieldName, bool hideDeleted)
        {
            return Controller.GetAll(orderFieldName, hideDeleted);
        }

        public T TransactionOverlay<T>(TransactionOverlayDeleagate<T> executingMethod)
        {
            return Controller.TransactionOverlay(executingMethod);
        }
        public void TransactionOverlay(TransactionOverlayDeleagate executingMethod)
        {
            Controller.TransactionOverlay(executingMethod);
        }
    }
}
